package cloudnative.demo.labservice.controller;
import cloudnative.demo.labservice.mapper.PersonnelMapper;
import cloudnative.demo.labservice.model.Manager;
import cloudnative.demo.labservice.model.Student;
import cloudnative.demo.labservice.model.Teacher;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/personnel")
@AllArgsConstructor
@Slf4j
public class PersonnelController {

    @Autowired
    private PersonnelMapper personnelMapper;


    // 增加教师接口
    @PostMapping("/addT")
    public void addTeacher(@RequestBody Teacher teacher) {
        personnelMapper.addTeacher(teacher.getName(), teacher.getMobileNumber());
    }

    // 增加学生接口
    @PostMapping("/addS")
    public void addStudent(@RequestBody Student student) {
        personnelMapper.addStudent(student.getName(), student.getMobileNumber());
    }

    // 增加管理员接口
    @PostMapping("/addM")
    public void addManager(@RequestBody Manager manager) {
        personnelMapper.addManager(manager.getName(), manager.getMobileNumber());
    }

    // 查询所有教师
    @GetMapping("/queryT")
    public List<Teacher> queryAllTeachers() {
        try{
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("-------------OK   //queryT--------------------");
        return personnelMapper.queryAllTeachers();
    }

    // 查询所有学生
    @GetMapping("/queryS")
    public List<Student> queryAllStudents() {
        try{
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("-------------OK   //queryS--------------------");
        return personnelMapper.queryAllStudents();
    }

    // 查询所有管理员
    @GetMapping("/queryM")
    public List<Manager> queryAllManagers() {
        try{
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("-------------OK   //queryM--------------------");
        return personnelMapper.queryAllManagers();
    }

    // 按名字和ID查询学生
    @PostMapping("/querySBNID")
    public List<Student> queryStudentByNameAndId(@RequestBody Student student) {
        List<Student> result = new ArrayList<Student>();
        if (student.getStudentID() != null && student.getName() == null) {
            result.add(personnelMapper.findStudentById(student.getStudentID()));
            return result;
        } else if (student.getName() != null && student.getStudentID() == null) {
            return personnelMapper.findStudentByName(student.getName());
        } else if (student.getStudentID() != null && student.getName() != null) {
            result.add(personnelMapper.findStudentByNameAndId(student.getName(), student.getStudentID()));
            return result;
        } else {
            return result;
        }
    }

    // 按名字和ID查询管理员
    @PostMapping("/queryMBNID")
    public List<Manager> queryManagerByNameAndId(@RequestBody Manager manager) {
        List<Manager> result = new ArrayList<>();
        if (manager.getWorkerID() != null && manager.getName() == null) {
            result.add(personnelMapper.findManagerById(manager.getWorkerID()));
            return result;
        } else if (manager.getName() != null && manager.getWorkerID() == null) {
            return personnelMapper.findManagerByName(manager.getName());
        } else if (manager.getWorkerID() != null && manager.getName() != null) {
            result.add(personnelMapper.findManagerByNameAndId(manager.getName(), manager.getWorkerID()));
            return result;
        } else {
            return result;
        }
    }

    // 按名字和ID查询教师
    @PostMapping("/queryTBNID")
    public List<Teacher> queryTeacherByNameAndId(@RequestBody Teacher teacher) {
        List<Teacher> result = new ArrayList<>();
        if (teacher.getTeacherID() != null && teacher.getName() == null) {
            result.add(personnelMapper.findTeacherById(teacher.getTeacherID()));
            return result;
        } else if (teacher.getName() != null && teacher.getTeacherID() == null) {
            return personnelMapper.findTeacherByName(teacher.getName());
        } else if (teacher.getTeacherID() != null && teacher.getName() != null) {
            result.add(personnelMapper.findTeacherByNameAndId(teacher.getName(),teacher.getTeacherID()));
            return result;
        } else {
            return result;
        }
    }

    // 删除教师接口
    @PostMapping("/deleteT")
    public void deleteTeacher(@RequestBody Teacher teacher) {
        personnelMapper.deleteTeacher(teacher.getTeacherID());
    }

    // 删除学生接口
    @PostMapping("/deleteS")
    public void deleteStudent(@RequestBody Student student) {
        personnelMapper.deleteStudent(student.getStudentID());
    }

    // 删除管理员接口
    @PostMapping("/deleteM")
    public void deleteManager(@RequestBody Manager manager) {
        personnelMapper.deleteManager(manager.getWorkerID());
    }

    // 更新教师信息
    @PostMapping("/alterT")
    public void updateTeacher(@RequestBody Teacher teacher) {
        personnelMapper.updateTeacher(teacher.getTeacherID(), teacher.getName(), teacher.getMobileNumber());
    }

    // 更新学生信息
    @PostMapping("/alterS")
    public void updateStudent(@RequestBody Student student) {
        personnelMapper.updateStudent(student.getStudentID(), student.getName(), student.getMobileNumber());
    }

    // 更新管理员信息
    @PostMapping("/alterM")
    public void updateManager(@RequestBody Manager manager) {
        personnelMapper.updateManager(manager.getWorkerID(), manager.getName(), manager.getMobileNumber());
    }
}